﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Whir_System_Module_Template_SetFields : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropM();
        }
    }

    //绑定功能模块
    protected void BindDropM()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("modelID,modelName,tableName", "Whir_Model", "", "");

        this.ddlModel.Items.Clear();
        this.ddlModel.Items.Add(new ListItem("= 功能模块列表 =", "-1"));
        foreach (DataRow dr in dt.Rows)
            this.ddlModel.Items.Add(new ListItem(dr["modelName"].ToString(), dr["modelID"].ToString() + "|" + dr["tableName"].ToString()));
    }

    //绑定表单
    public void BindDropF()
    {
        string pid = this.ddlModel.SelectedValue.Split('|')[0];
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("modelFormID,fieldIDs,formName", "Whir_ModelForm", "modelID=" + pid, "");

        this.ddlForm.Items.Clear();
        this.ddlForm.Items.Add(new ListItem("= 表单列表 =", "-1"));
        foreach (DataRow dr in dt.Rows)
            this.ddlForm.Items.Add(new ListItem(dr["formName"].ToString(), dr["modelFormID"].ToString() + "_" + dr["fieldIDs"].ToString().Replace("|true", "").Replace("|false", "")));
    }

    //绑定字段
    public void BindCbl()
    {
        string ids = this.ddlForm.SelectedValue.Split('_')[1];
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("fieldName,alias", "Whir_Field", "fieldID in (" + ids + ")", "");

        this.cblField.Items.Clear();

        Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
        Whir.Model.Whir_Model model = bll.GetModel(int.Parse(this.ddlModel.SelectedValue.Split('|')[0]));

        string primarkKeyName = ContentManage.GetCustomTablePrimaryKeyName(model.tableName);
        string label = "{$" + primarkKeyName + "}";
        this.cblField.Items.Add(new ListItem(primarkKeyName + "  主键 " + "<a onclick=\"copyUrl('" + label + "');\">复制</a>", ""));

        string text = "";
        string value = "";
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            value = "{$" + dt.Rows[i]["fieldName"].ToString() + "}";
            text = dt.Rows[i]["fieldName"].ToString() + " " + dt.Rows[i]["alias"].ToString() + " " + "<a onclick=\"copyUrl('" + value + "');\">复制</a>";
            this.cblField.Items.Add(new ListItem(text, value));
        }

        foreach (ListItem item in this.cblField.Items)
        {
            item.Selected = true;
        }
    }

    //功能模块事件
    protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlModel.SelectedIndex == 0)
        {
            this.ddlForm.Items.Clear();
            this.cblField.Items.Clear();
            return;
        }

        BindDropF();
    }

    //表单事件
    protected void ddlForm_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlForm.SelectedIndex == 0)
        {
            this.cblField.Items.Clear();
            return;
        }

        BindCbl();
    }
}
